﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using System.Collections;
using ESRI.ArcGIS.SystemUI;

namespace Homework2
{
    public partial class Form1 : Form
    {
        private ILayer m_Layer;
        private ArrayList list = new ArrayList();
        public Form1()
        {
            InitializeComponent();
        }

       

      

        private void axLicenseControl1_Enter(object sender, EventArgs e)
        {

        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
        {
            if (e.button == 2)
            {
                ESRI.ArcGIS.Controls.esriTOCControlItem Item = ESRI.ArcGIS.Controls.esriTOCControlItem.esriTOCControlItemNone;
                IBasicMap pBasicMap = null;
                ILayer pLayer = null;
                object other = null;
                object index = null;
                axTOCControl1.HitTest(e.x, e.y, ref Item, ref pBasicMap, ref pLayer, ref other, ref index);          //实现赋值
                m_Layer = pLayer;
                if (Item == esriTOCControlItem.esriTOCControlItemLayer)           //点击的是图层的话，就显示右键菜单
                {
                    contextMenuStrip1.Show(axTOCControl1, new System.Drawing.Point(e.x, e.y));
                    //显示右键菜单，并定义其相对控件的位置，正好在鼠标出显示
                }
            }
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            //axTOCControl1.Width = this.Width / 5 * 2;
            ////axTOCControl1.Height = this.Height / 3 * 2;
            //axToolbarControl1.Width = this.Width / 5 * 3;
            ////axToolbarControl1.Height = this.Height;
            //groupBox1.Width = this.Width / 5 * 2;
            ////groupBox1.Height = this.Height / 3;
            //tabControl1.Width = this.Width / 5 * 2;
            ////tabControl1.Height = this.Height;
        }

        private void 删除图层ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < axMapControl1.LayerCount; i++)
            {
                 if (axMapControl1.get_Layer(i) == m_Layer)//通过for循环得到选中图层索引，并直接调用DeleteLayer方法定点删除
                 axMapControl1.DeleteLayer(i);
            }
            axMapControl1.ActiveView.Refresh();
        }

        private void 文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IMap pMap = axMapControl1.Map;
            int toIndex = 0;
            for (int i = 0; i < axMapControl1.LayerCount; i++)
            {
                
                if (axMapControl1.get_Layer(i) == m_Layer)//通过for循环得到选中图层索引，并直接调用DeleteLayer方法定点删除
                    toIndex = i;
            }
            if (toIndex > 1)
            {
                toIndex--;//上移减一
                pMap.MoveLayer(m_Layer, toIndex);
                axMapControl1.ActiveView.Refresh();
                axTOCControl1.Update();
            }
        }

        private void 下移图层ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IMap pMap = axMapControl1.Map;
            int toIndex = 0;
            for (int i = 0; i < axMapControl1.LayerCount; i++)
            {

                if (axMapControl1.get_Layer(i) == m_Layer)//通过for循环得到选中图层索引，并直接调用DeleteLayer方法定点删除
                    toIndex = i;
            }
            if (toIndex < axMapControl1.LayerCount)
            {
                toIndex++;//下移加一
                pMap.MoveLayer(m_Layer, toIndex);
                axMapControl1.ActiveView.Refresh();
                axTOCControl1.Update();
            }
        }

        private void 文件ToolStripMenuItem1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            axToolbarControl1.AddMenuItem("esriControls.ControlsMapViewMenu", -1, false, -1);

        }

        
/*
 *
 * 实验三代码部分
 * 
 */
        /*
         * 地图取点
         */
        private void button1_Click(object sender, EventArgs e)
        {
            radioButton1.Enabled = true;
            radioButton2.Enabled = true;
            radioButton3.Enabled = true;
        }
        //当点击了start按钮后，开始记录点击的点坐标
        private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {
            if (button1.Enabled)
            {
                //if（radioButton1.Enabled || radioButton2.Enabled || radioButton3.Enabled）
                {
                    Console.WriteLine("MapX:" + e.mapX);
                    Console.WriteLine("MapY:" + e.mapY);
                    Console.WriteLine("X:" + e.x);
                    Console.WriteLine("Y:" + e.y);
                    list.Add(e);
                }
            }
           
        }
        //点击stop按钮后，显示产生的点、多义线、多边形
        private void button2_Click(object sender, EventArgs e)
        {
            radioButton1.Enabled = false;
            radioButton2.Enabled = false;
            radioButton3.Enabled = false;
            //创建多个点
            if (radioButton1.Checked)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    IMapControlEvents2_OnMouseDownEvent even = (IMapControlEvents2_OnMouseDownEvent)list[i];
                    IPoint point = new PointClass();
                    point.X = even.x;
                    point.Y = even.y;
                }
                IMapControlEvents2_OnMouseDownEvent eve = (IMapControlEvents2_OnMouseDownEvent)list[0];
                String message = "生成了" + list.Count + "个Point";
                MessageBox.Show(message);
            }
            //创建一条多义线
            else if (radioButton2.Checked)
            {
                ISegmentCollection path = new PathClass();
                IGeometryCollection polyline = new PolylineClass();
                for (int i = 1; i < list.Count; i = i + 2)
                {
                    IMapControlEvents2_OnMouseDownEvent even1 = (IMapControlEvents2_OnMouseDownEvent)list[i - 1];
                    IMapControlEvents2_OnMouseDownEvent even2 = (IMapControlEvents2_OnMouseDownEvent)list[i];
                    IPoint point1 = new PointClass();
                    IPoint point2 = new PointClass();
                    point1.PutCoords(even1.x, even1.y);
                    point2.PutCoords(even2.x, even2.y);
                    ILine line = new LineClass();
                    line.PutCoords(point1, point2);
                    object Missing1 = Type.Missing;
                    object Missing2 = Type.Missing;
                    path.AddSegment(line as ISegment, ref Missing1, ref Missing2);
                    polyline.AddGeometry(path as IGeometry, ref Missing1, ref Missing2);

                }
                if (polyline != null)
                {
                    String message = "生成了1个polyline";
                    MessageBox.Show(message);
                }
            }
            //创建一个多边形
            else if (radioButton3.Checked)
            {
                Ring pRing = new RingClass();
                IPointCollection iPointCollection = new PolygonClass();
                object missing = Type.Missing;
                for (int i = 0; i < list.Count; i = i + 1)
                {
                    IMapControlEvents2_OnMouseDownEvent even1 = (IMapControlEvents2_OnMouseDownEvent)list[i];
                    IPoint pPoint1 = new PointClass();
                    pPoint1.PutCoords(even1.x, even1.y);
                    iPointCollection.AddPoint(pPoint1, ref missing, ref missing);
                }
                pRing.AddPointCollection(iPointCollection);
                IGeometryCollection polygon = new PolygonClass();
                polygon.AddGeometry(pRing as IGeometry, ref missing, ref missing);
                IPolygon polyGonGeo = polygon as IPolygon;

            }


            //最后将列表清空
            list = new ArrayList();
        }
        /*
         * 缩放地图
         */
        private void button3_Click(object sender, EventArgs e)
        {
            radioButton4.Enabled = true;
            radioButton5.Enabled = true;
        }
        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton4.Checked)
            {
                ICommand pCommand = new ControlsMapZoomInToolClass();
                pCommand.OnCreate(axMapControl1.Object);
                axMapControl1.CurrentTool = pCommand as ITool;

            }
        }
        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton5.Checked)
            {
                ICommand pCommand = new ControlsMapZoomOutToolClass();
                pCommand.OnCreate(axMapControl1.Object);
                axMapControl1.CurrentTool = pCommand as ITool;

            }
        }

        //结束
        private void button4_Click(object sender, EventArgs e)
        {
            radioButton4.Enabled = false;
            radioButton5.Enabled = false;
        }

        
        
            


    }
}
